package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import com.xinghou.platform.util.PhotoPathUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.SharedCardImageDAO;
import com.xinghou.platform.entity.resp.ShareInfoPhotoResp;
import com.xinghou.platform.model.main.SharedCardImage;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.StringUtils;
/**
 * @author liuwei.wen
 *
 */
@Service
public class SharedCardImageDAOImpl  {/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private static String photominuri = ConfigUtil.getProperty("file_download");
	private static String photouri = ConfigUtil.getProperty("file_download");
	
	@Override
	public SharedCardImage getSharedCardImageByid(Integer id)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT Id,User_id,Card_id,Uri,STATUS,Praise_count,Create_time FROM shared_card_image where Id =?";
		SharedCardImage sharedCardImage = jdbcTemplate.queryForObject(sql, new Object[] {id}, new SharedCardImageMapper());
		CustomerContextHolder.clearCustomerType();
		return sharedCardImage;
	}

	@Override
	public int updateSharedCardImage(SharedCardImage sharedCardImage)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE shared_card_image SET User_id =?,Card_id=?,Uri =?,STATUS=?,Praise_count=?,Create_time=? Where Id =?"	
				);
		Object[] params = {sharedCardImage.getUserId(),sharedCardImage.getCardId(),sharedCardImage.getUri(),sharedCardImage.getStatus(),sharedCardImage.getPraiseCount(), sharedCardImage.getCreateTime(),sharedCardImage.getId()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	public int deleteSharedCardImage(SharedCardImage sharedCardImage)throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"DELETE FROM shared_card_image WHERE Id = ? AND Card_id = ?"	
				);
		Object[] params = {sharedCardImage.getId(),sharedCardImage.getCardId()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int saveSharedCardImage(SharedCardImage sharedCardImage)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO shared_card_image (User_id,Card_id,Uri,STATUS,Praise_count,Create_time)VALUES(?,?,?,?,?,?)"
				);
		Object[] params = {sharedCardImage.getUserId(),sharedCardImage.getCardId(),sharedCardImage.getUri(),sharedCardImage.getStatus(),sharedCardImage.getPraiseCount(),sharedCardImage.getCreateTime()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public List<SharedCardImage> getSharedCardImagelistByUserId(int UserId)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT Id,User_id,Card_id,Uri,STATUS,Praise_count,Create_time FROM shared_card_image where User_id =?";
		List<SharedCardImage> list = jdbcTemplate.query(sql, new Object[] {UserId}, new SharedCardImageMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	private static final class SharedCardImageMapper implements RowMapper<SharedCardImage> {
		public SharedCardImage mapRow(ResultSet rs, int rowNum) throws SQLException {
			SharedCardImage sharedCardImage = new SharedCardImage();
			sharedCardImage.setCardId(rs.getInt("Card_id"));
			sharedCardImage.setCreateTime(rs.getTimestamp("Create_time"));
			sharedCardImage.setId(rs.getInt("Id"));
			sharedCardImage.setPraiseCount(rs.getInt("Praise_count"));
			sharedCardImage.setStatus(rs.getByte("STATUS"));
			sharedCardImage.setUri(rs.getString("Uri"));
			sharedCardImage.setUserId(rs.getInt("User_id"));
			return sharedCardImage;
		}
	}
	
	public List<ShareInfoPhotoResp> getSharedCardImagelist(int cardid ) throws DataAccessException{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		 String sql = "SELECT Id,User_id,Card_id,Uri,STATUS,Praise_count,Create_time FROM shared_card_image WHERE STATUS>0 and Card_id = ?";
		  List<ShareInfoPhotoResp> list=(List<ShareInfoPhotoResp>)jdbcTemplate.query(sql,new Object[] {cardid},new ShareInfoPhotoRespListMapper());
		return list;
	}
	
	private static final class ShareInfoPhotoRespListMapper implements RowMapper<ShareInfoPhotoResp> {
		public ShareInfoPhotoResp mapRow(ResultSet rs, int rowNum) throws SQLException {
			ShareInfoPhotoResp shareInfoPhotoResp = new ShareInfoPhotoResp();
			shareInfoPhotoResp.setPhotoid(rs.getInt("Id")+"");
			shareInfoPhotoResp.setShareid(rs.getInt("Card_id")+"");
			shareInfoPhotoResp.setPhotourl(photouri+rs.getString("Uri"));
			shareInfoPhotoResp.setSimphotourl(PhotoPathUtil.getDB160(photouri+rs.getString("Uri")));
			return shareInfoPhotoResp;
		}
	}

	@Override
	public SharedCardImage getSharedCardImageByCardId(int cardid)throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT Id,User_id,Card_id,Uri,STATUS,Praise_count,Create_time FROM shared_card_image where Card_id =? and image_type=1";
		SharedCardImage sharedCardImage = jdbcTemplate.queryForObject(sql, new Object[] {cardid}, new SharedCardImageMapper());
		CustomerContextHolder.clearCustomerType();
		return sharedCardImage;
	}

	@Override
	public SharedCardImage getMinTimeSharedCardImageByCardId(int cardid)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT Id,User_id,Card_id,Uri,STATUS,Praise_count,Create_time FROM shared_card_image where Card_id =? ORDER BY Create_time LIMIT 1";
		SharedCardImage sharedCardImage = jdbcTemplate.queryForObject(sql, new Object[] {cardid}, new SharedCardImageMapper());
		CustomerContextHolder.clearCustomerType();
		return sharedCardImage;
	}
*/
}
